package com.finance.cooperate.dao.config;

import com.finance.cooperate.dao.support.jpa.CooperateConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.DefaultConversionService;

import javax.sql.DataSource;

/**
 * @ClassName DataSourceConfig
 * @Description 数据源配置
 * @Author shen
 * @Date 2023/8/12 10:16
 * @Modify ...
 */
@Configuration
public class DataSourceConfig {

    public DataSourceConfig() {
        ((DefaultConversionService) DefaultConversionService.getSharedInstance())
                .addConverter(new CooperateConverter());
    }

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.primary")  //  读取配置文件主数据源参数
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")  //  读取配置文件副数据源参数
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }


}
